﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Timer
{
    public partial class Main : Form
    {
        private bool _exitBtnClicked = false;

      

        public Main()
        {
            InitializeComponent();
            
            this.WorkTimeTextBox.Text = (Program.Timer.IntervalTime.WorkTime.Minutes).ToString();
            this.RestTimeTextBox.Text = (Program.Timer.IntervalTime.RestTime.Minutes).ToString();
      

            Program.Timer.Tick += new EventHandler<TomatoTimerElapsedEventArgs>(Timer_Tick);
            Program.Timer.RestEnded += new EventHandler(Timer_RestEnded);
            Program.Timer.WorkEnded += new EventHandler(Timer_WorkEnded);
            
        }

        void Timer_WorkEnded(object sender, EventArgs e)
        {
            
            ShowMessage("Let's take a rest!");
        }

        void Timer_RestEnded(object sender, EventArgs e)
        {
            
            ShowMessage("Let's work!");
        }

        void ShowMessage(string message)
        {
            if (notifyIcon1.Visible)
            {
                notifyIcon1.ShowBalloonTip(1000, "", message, ToolTipIcon.None);
            }
            else
            {
                MessageBox.Show(message);
            }
        }

        void Timer_Tick(object sender, TomatoTimerElapsedEventArgs e)
        {
            //this.Invoke(updateText, e.TimeReminaing);
            this.workTimeRemain.Text = e.TimeReminaing.WorkTime.ToString(@"mm\:ss");
            this.restTimeRemain.Text = e.TimeReminaing.RestTime.ToString(@"mm\:ss");
        }

        void UpdateTexts(TomatoTime _currentTime)
        {
            
            
        }

        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = !_exitBtnClicked;
            if (!_exitBtnClicked)
            {
                this.ShowInTaskbar = false;
                this.Hide();
            }
            
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
        }

        private void Main_Shown(object sender, EventArgs e)
        {
            this.ShowInTaskbar = true;
           
        }

       

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _exitBtnClicked = true;
            this.Close();
        }

        private void ExitButton_Click(object sender, EventArgs e)
        {
            _exitBtnClicked = true;
            this.Close();
        }

        private void Main_VisibleChanged(object sender, EventArgs e)
        {
            notifyIcon1.Visible = !this.Visible;
        }

        private void showToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
        }

        private void SettingsButton_Click(object sender, EventArgs e)
        {
            SettingForm f = new SettingForm();
            DialogResult r = f.ShowDialog();
            if (r == DialogResult.OK)
            {
                Program.Timer.Stop();

            }
        }

        private void StartButton_Click(object sender, EventArgs e)
        {
            Program.Timer.Start();
        }

        private void StopButton_Click(object sender, EventArgs e)
        {
            Program.Timer.Stop();
        }

        private void restartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.Timer.Restart();
            
        }
    }
}
